package de.tc.stammdatenverwaltung.gui.kontakthistory;

import de.tc.stammdatenverwaltung.functions.commonFunctions;
import de.tc.stammdatenverwaltung.gui.Main;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class showKontaktEintrag extends javax.swing.JDialog {

  private int nID = -999; //NachrichtenID
  private int kID = -999; //NachrichtenID

  public showKontaktEintrag(java.awt.Frame parent, boolean modal, int ID, int kid)
  {
    super(parent, modal);
    initComponents();
    nID = ID;
    kID = kid;
    String abfrage = "select sd.VNAME, sd.NNAME, k.DATUM, k.ART, k.EIN, k.NACHRICHT" +
        " from KONTAKTHISTORIE k " + " left join STAMMDATEN sd on sd.id = k.IDKONTAKT " +
        " where k.id = " + ID;
    try
    {
      Statement stmt = Main.con.createStatement();
      ResultSet rs = stmt.executeQuery( abfrage );

      if (rs.isBeforeFirst())
        rs.next();

      jLabel5.setText( rs.getString("VNAME") + " " + rs.getString("NNAME") );
      jLabel6.setText( rs.getString("DATUM") );
      jLabel7.setText( getArt( rs.getInt("ART"), rs.getInt("EIN") ) );
      jTextArea1.setText( rs.getString("NACHRICHT") );
    }
    catch (SQLException e)
    {
      JOptionPane.showMessageDialog(null, "Fehler beim abrufen der Details:\r\n" + e.getLocalizedMessage(),
                "Fehler!", 1);
      System.out.println( abfrage );
      e.printStackTrace();
    }

  }

  private String getArt(int a, int e)
  {
    String temp = commonFunctions.nachrichtArt[a];

    temp += ", " + (e==0?"eingehend":"ausgehend");
    return temp;
  }

    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("Betrifft");

    jLabel2.setText("Datum");

    jLabel3.setText("Art");

    jLabel4.setText("Inhalt");

    jTextArea1.setColumns(20);
    jTextArea1.setEditable(false);
    jTextArea1.setRows(5);
    jTextArea1.setTabSize(2);
    jScrollPane1.setViewportView(jTextArea1);

    jLabel5.setText("jLabel5");

    jLabel6.setText("jLabel6");

    jLabel7.setText("jLabel7");

    jButton1.setText("Eintrag löschen");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
          .addComponent(jLabel4)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel1)
              .addComponent(jLabel2)
              .addComponent(jLabel3))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel7)
              .addComponent(jLabel6)
              .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 280, Short.MAX_VALUE)
                .addComponent(jButton1)))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(jLabel5)
          .addComponent(jButton1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(jLabel6))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(jLabel7))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      String deleteQ = "delete from KONTAKTHISTORIE where ID = " + nID;
      try
      {
        if (nID > -999)
        {
          Statement stmt = Main.con.createStatement();
          stmt.executeUpdate(deleteQ);

          commonFunctions.load_query_to_table(Kontakthistorie.selectSQL+ "where IDKONTAKT = " + kID + " order by datum desc",
            Kontakthistorie.jTable1, Kontakthistorie.dtm );

          JOptionPane.showMessageDialog(null, "Nachrich wurde erfolgreich gelöscht.",
                  "Fehler!", JOptionPane.PLAIN_MESSAGE);

          this.dispose();
        }
      }
      catch (SQLException e)
      {
        JOptionPane.showMessageDialog(null, "Fehler beim abrufen der Details:\r\n" + e.getLocalizedMessage(),
                  "Fehler!", 1);
        System.out.println( deleteQ );
        e.printStackTrace();
      }

    }//GEN-LAST:event_jButton1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  // End of variables declaration//GEN-END:variables
}
